package com.jshongli.myapplication.common.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jshongli.myapplication.R;


@SuppressWarnings("RestrictedApi")
public class CustomToolbar extends Toolbar implements View.OnTouchListener{

    private Context mContext;

    private LayoutInflater mInflater;
    private View mView;
    private TextView tvTitle;
    private ImageView ivLeft;
    private TextView tvRight;


    public CustomToolbar(Context context) {
        this(context,null);
    }

    public CustomToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();

        if (attrs != null) {
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(mContext, attrs, R.styleable.CustomToolbar, defStyleAttr, 0);

            CharSequence title = a.getText(R.styleable.CustomToolbar_title);
            if (title != null) {
                setTitle(title);
            }

            CharSequence textRight = a.getText(R.styleable.CustomToolbar_tvRight_text);
            if (textRight != null) {
                setTvRightText(textRight);
            }

            final Drawable drawableRight = a.getDrawable(R.styleable.CustomToolbar_tvRight_drawable);
            if (drawableRight != null) {
                setTvRightDrawable(drawableRight);
            }


        }

    }

    private void init() {

        if (mView == null) {

            mInflater = LayoutInflater.from(mContext);
            mView = mInflater.inflate(R.layout.widget_custom_toolbar, null);
            ivLeft = (ImageView) mView.findViewById(R.id.iv_left);
            tvTitle = (TextView) mView.findViewById(R.id.tv_title);
            tvRight = (TextView) mView.findViewById(R.id.tv_right);

            ivLeft.setOnTouchListener(this);
            tvRight.setOnTouchListener(this);

            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
            addView(mView, lp);
        }
    }

//    public void setTvLeftText(CharSequence text) {
//        tvLeft.setText(text);
//        tvLeft.setVisibility(VISIBLE);
//    }

    public void setTvLeftText(int resId) {
        setTvRightText(getResources().getString(resId));
    }

    public ImageView getIvLeft() {

        return this.ivLeft;
    }

    public void setTvRightText(CharSequence text) {
        tvRight.setText(text);
        tvRight.setVisibility(VISIBLE);
    }

    public void setTvRightText(int resId) {
        setTvRightText(getResources().getString(resId));
    }

    public TextView getTvRight() {

        return this.tvRight;
    }

    public void setTvRightDrawable(Drawable rightDrawable) {
        tvRight.setCompoundDrawables(null,null,rightDrawable,null);
    }


    @Override
    public void setTitle(@StringRes int resId) {
        setTitle(mContext.getText(resId));
    }

    @Override
    public void setTitle(CharSequence title) {

        if (tvTitle != null) {
            tvTitle.setText(title);
            showTvTitle();
        }
    }

    public void showTvTitle(){
        if(tvTitle !=null)
            tvTitle.setVisibility(VISIBLE);
    }


    public void hideTvTitle() {
        if (tvTitle != null)
            tvTitle.setVisibility(GONE);

    }

    public TextView getTvTitle() {

        return this.tvTitle;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()) {
//            case R.id.tv_left:
//                if (event.getAction() == MotionEvent.ACTION_DOWN) {
//                    tvLeft.setTextColor(getResources().getColor(R.color.alpha_white_80));
//                }else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() ==  MotionEvent.ACTION_CANCEL || event.getAction() ==  MotionEvent.ACTION_OUTSIDE) {
//                    tvLeft.setTextColor(getResources().getColor(R.color.white));
//                }
//                break;
            case R.id.tv_right:
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    tvRight.setTextColor(getResources().getColor(R.color.alpha_white_80));
                }else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() ==  MotionEvent.ACTION_CANCEL || event.getAction() ==  MotionEvent.ACTION_OUTSIDE) {
                    tvRight.setTextColor(getResources().getColor(R.color.white));
                }
                break;
        }
        return false;

    }

}
